<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>欢迎登陆</title>
    <link rel="stylesheet" href="../layui/layui/css/layui.css">
</head>
<body>

<div id="login" >
    <h2 align="center" id="h2">欢迎登陆</h2>
    <form class="layui-form" action="/user/login">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="real_name" lay-verify="title" autocomplete="off" placeholder="请输入您的用户名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" lay-verify="pass" placeholder="请输入您的密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">验证码</label>
            <div class="layui-input-inline">
                <input type="text" name="cap" lay-verify="required" placeholder="请输入验证码" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline">
                <img src="/code"  style="width:100px" id="cap"/>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>

<script src="../layui/layui/layui.js"></script>
<script>
    layui.use(function(){
        var layer = layui.layer,
            form = layui.form,
            $=layui.$;
        $("#cap").click(function(){
            //刷新验证码
            path = $(this).attr("src")+"?"+new Date().getTime();
            $(this).attr("src",path);
        });

            form.on('submit(demo)', function(data){
                //debugger //这个代码代表可以在前端的这行代码停住
                $.ajax({
                    url:"/user/login",
                    type:"post",
                    data:data.field,//代表获取表单中的数据
                    success:function (result) {
                        if (result.status==1005){
                            window.location.href="../index.html";
                        }else {
                            layer.alert(result.message,{
                                title:'提示信息'
                            });
                        }
                    }
                });
                return false;
            });
        })
</script>

<style>
    #cap{
        margin-left: 60px;
        margin-bottom: 10px;
    }
    #h2{
        padding-bottom: 20px;
    }

    #login {
        margin: 100px auto;
        /*这个div距离父块的距离 距离上面100px，其他的居中*/
        /*border: 1px solid red;*/
        width: 600px;
        height: 400px;
        padding: 20px 20px 20px 20px;
        /*    这个div距离第一行内容的距离（向右下左）*/
        box-shadow: 15px 15px 15px 15px #888888;
    }
</style>
</body>
</html>
